package com.ruoyi.nmy.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.nmy.mapper.TransactionMapper;
import com.ruoyi.nmy.domain.Transaction;
import com.ruoyi.nmy.service.ITransactionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试红冲列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-24
 */
@Service
@Transactional
public class TransactionServiceImpl implements ITransactionService 
{
    @Autowired
    private TransactionMapper transactionMapper;


    /**
     * 查询测试红冲列表
     * 
     * @param id 测试红冲列表主键
     * @return 测试红冲列表
     */
    @Override
    public Transaction selectTransactionById(Long id)
    {
        return transactionMapper.selectTransactionById(id);
    }

    /**
     * 查询测试红冲列表列表
     * 
     * @param transaction 测试红冲列表
     * @return 测试红冲列表
     */
    @Override
    public List<Transaction> selectTransactionList(Transaction transaction)
    {
        return transactionMapper.selectTransactionList(transaction);
    }

    /**
     * 新增测试红冲列表
     * 
     * @param transaction 测试红冲列表
     * @return 结果
     */
    @Override
    public int insertTransaction(Transaction transaction)
    {
        return transactionMapper.insertTransaction(transaction);
    }

    /**
     * 修改测试红冲列表
     *
     * @param transaction 测试红冲列表
     * @return 结果
     */

    @Override
    public AjaxResult updateTransaction(Transaction transaction)
    {
        // 根据id获取信息
        Transaction originalTransaction = transactionMapper.selectTransactionById(transaction.getId());
        if (originalTransaction == null) {
            return AjaxResult.error("未找到该条数据");
        }

        BigDecimal originalDebitAmount = originalTransaction.getdPrice();
        BigDecimal originalCreditAmount = originalTransaction.getjPrice();
        BigDecimal newDebitAmount = transaction.getdPrice();
        BigDecimal newCreditAmount = transaction.getjPrice();

        if (newDebitAmount.compareTo(newCreditAmount) != 0) {
            return AjaxResult.error("借贷不平衡");
        }

        Transaction redVoidTransaction = new Transaction();
        redVoidTransaction.setName(transaction.getName());
        redVoidTransaction.setBh("冲销" + transaction.getBh());
        redVoidTransaction.setSubject(transaction.getSubject());
        redVoidTransaction.setcTime(new Date());
        redVoidTransaction.setStatus(1L);

        if (originalDebitAmount.compareTo(newDebitAmount) != 0 && originalCreditAmount.compareTo(newCreditAmount) != 0) {
            redVoidTransaction.setjPrice(newCreditAmount);
            redVoidTransaction.setdPrice(newDebitAmount);
        } else {
            redVoidTransaction.setjPrice(newCreditAmount.negate());
            redVoidTransaction.setdPrice(newDebitAmount.negate());
        }

        transactionMapper.insertTransaction(redVoidTransaction);

        originalTransaction.setStatus(2L);
        transactionMapper.updateTransaction(originalTransaction);
        return AjaxResult.success();
    }

    /**
     * 批量删除测试红冲列表
     * 
     * @param ids 需要删除的测试红冲列表主键
     * @return 结果
     */
    @Override
    public int deleteTransactionByIds(Long[] ids)
    {
        return transactionMapper.deleteTransactionByIds(ids);
    }

    /**
     * 删除测试红冲列表信息
     * 
     * @param id 测试红冲列表主键
     * @return 结果
     */
    @Override
    public int deleteTransactionById(Long id)
    {
        return transactionMapper.deleteTransactionById(id);
    }




}
